package com.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.web.entity.DiseaseLibrary;
import com.web.mapper.DiseaseLibraryMapper;
import com.web.service.DiseaseLibraryService;
import org.springframework.stereotype.Service;

@Service
public class DiseaseLibraryServiceImpl extends ServiceImpl<DiseaseLibraryMapper, DiseaseLibrary>
        implements DiseaseLibraryService {

    @Override
    public IPage<DiseaseLibrary> getLibraryList(int page, int size, String type, String keyword) {
        Page<DiseaseLibrary> pageParam = new Page<>(page, size);
        QueryWrapper<DiseaseLibrary> wrapper = new QueryWrapper<>();

        // 按类型筛选
        if (StringUtils.isNotBlank(type) && !"all".equals(type)) {
            wrapper.eq("type", type);
        }

        // 按关键词搜索
        if (StringUtils.isNotBlank(keyword)) {
            wrapper.and(w -> w.like("name", keyword)
                    .or().like("english_name", keyword)
                    .or().like("crops", keyword));
        }

        wrapper.orderByDesc("create_time");
        return this.page(pageParam, wrapper);
    }

    @Override
    public DiseaseLibrary getLibraryDetail(Long id) {
        return this.getById(id);
    }
}
